package com.mss.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mss.media.dto.CommentsPageDTO;
import com.mss.media.entity.MediaComments;
import com.mss.media.mapper.MediaCommentsMapper;
import com.mss.media.service.IMediaCommentsService;
import com.mss.media.util.abnormal.MssException;
import com.mss.media.vo.CommentsAuditPageVO;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author 湫
 * @Date 2021/12/28 11:39
 * Name MediaCommentsServiceImpl
 */
@Service
public class MediaCommentsServiceImpl extends ServiceImpl<MediaCommentsMapper, MediaComments> implements IMediaCommentsService {

    @Autowired
    private MediaCommentsMapper mediaCommentsMapper;
    /**
     * 评论分页信息
     * @param dto 筛选条件
     * @return 分页数据
     */
    @Override
    public IPage<CommentsAuditPageVO> findCommentsPage(CommentsPageDTO dto) {
        if (ObjectUtils.isEmpty(dto)) {
            dto = new CommentsPageDTO();
            dto.setLimit(10)
                    .setPage(1);
        }
        if (dto.getLimit() == null) {
            dto.setLimit(10);
        }
        if (dto.getPage() == null) {
            dto.setPage(1);
        }
        Page<CommentsAuditPageVO> page = new Page<>(dto.getPage(),dto.getLimit());
        return mediaCommentsMapper.findCommentsPage(dto.getCommentDate(), dto.getUserAccount(), page);
    }

    /**
     * 更改评论状态
     *
     * @param commentsId 评论主键
     * @param commentsStatus 状态
     * @return 状态
     */
    @Override
    public Boolean updateCommentsStatus(Long commentsId, Integer commentsStatus) {
        if (commentsId == null || commentsStatus == null) {
            throw new MssException("参数不能为空");
        }
        MediaComments mediaComments = mediaCommentsMapper.selectById(commentsId);
        if (ObjectUtils.isEmpty(mediaComments)) {
            throw new MssException("修改失败，未查询到相关评论信息");
        }
        LambdaUpdateWrapper<MediaComments> wrapper = Wrappers.lambdaUpdate();
        wrapper.set(MediaComments::getCommentsStatus,commentsStatus);
        wrapper.eq(MediaComments::getCommentsId,commentsId);
        int update = mediaCommentsMapper.update(mediaComments, wrapper);
        if (update <= 0) {
            throw new MssException("修改失败，系统错误");
        }
        return true;
    }
}
